package com.woniuxy.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.*;
import com.woniuxy.entity.vo.OrderVo;
import com.woniuxy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;



@Controller
@RequestMapping("order_vue")
public class OrdersController_vue {
    @Autowired
    private OrderService_vue orderService_vue;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CartService cartService;

    @PostMapping("add")
    @ResponseBody
    public ResponseEntity list(@RequestBody OrderVo orderVo, HttpSession session) {
        //获取用户ID
        User user = (User) session.getAttribute("user");
        Order order = new Order();

        //获取相应的数据
        //获取所选的地址ID
        Address address1 = orderVo.getAddress();

        System.out.println( address1.getId()+"+++++++++++++++++++++++++++++addressid");
        Address address = addressService.getByIdAddr(address1.getId());
        //获取留言
        String msg =  orderVo.getMsg();

        //获取支付方式
        String pay = orderVo.getPay();
        System.out.println(pay);
        //获取购物车ID     查找相对应的商品
        List<Cart> cartids = orderVo.getCarts();
        List<Cart> carts = new ArrayList<>();
        for (Cart cartid : cartids) {
            System.out.println(cartid.getId()+"+++++++++++++++++++++++++++++++cartid");
            carts.add(cartService.getByDish(user.getId(), cartid.getId()));
        }

        //计算总价
        BigDecimal zprice = new BigDecimal("0.00");
        for (Cart dish : carts) {
            System.out.println(dish.getNums() + "+++++++++" + "++++++++++" + dish.getDish().getPrice());
            BigDecimal num = new BigDecimal(dish.getNums() + "");
            BigDecimal price = new BigDecimal(dish.getMoney() + "");
            BigDecimal z = num.multiply(price);
            System.out.println(z);
            zprice = zprice.add(z);
            System.out.println(zprice);
        }
        //生成随机订单编号
        String times = DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDateTime.now());
        int a = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;
        order.setOrderNum("gfr" + times + a);
        order.setUserId(user.getId());
        order.setAddress(address.getProvince() + "" + address.getCity() + "" + address.getDistrict());
        order.setOrderTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
        order.setReciever(address.getPerson());
        order.setStatus("1");
        order.setOrderMsg(msg);
        order.setPhoneNum(address.getPhone());
        order.setPayWay(pay);
        order.setPrice(zprice);

        orderService_vue.add(order,carts);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(200);
        responseEntity.setData( "gfr" + times + a);
        return responseEntity;
    }


    @GetMapping("verify")
    public String verify(String number,HttpSession session,Model model){
        User user =(User) session.getAttribute("user");
        Order byOrder = orderService_vue.getByOrder(user.getId(), number);
        model.addAttribute("number",number);
        model.addAttribute("money",byOrder.getPrice());
        return "order_verify";
    }


    //支付成功修改订单状态
    @PostMapping("update/{number}")
    @ResponseBody
    public ResponseEntity update(@PathVariable String number, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println(number);
        orderService_vue.update(number, user.getId(), 2);
        return ResponseEntity.SUCCESS;
    }

   /* *//**
     * 我的订单    分页查询
     *//*
    @GetMapping("list")
    public String list(HttpSession session, Model model, Integer current) {
        User user = (User) session.getAttribute("user");
        Order order = new Order();
        order.setUserId(user.getId());
        if (current == null || current == 0) {
            current = 1;
        }
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        if(user.getId()!=0){
            queryWrapper.eq("user_id",user.getId());
        }
        Page<Order> page= new Page<>(current,2);
        model.addAttribute("page",orderService.page(page,queryWrapper));

        return "ucenter/user_orderlist";
    }

    *//**
     * 个人中心取消订单
     * @param id
     * @return
     *//*
    @GetMapping("del")
    public String del(int id){
        Order order = new Order();
        order.setId(id);
        order.setStatus("8");
        orderService.updateById(order);
        return "redirect:/order/list";
    }

    *//**
     * 个人中心订单支付
     * @param id
     * @return
     *//*
    @GetMapping("pay")
    public String pay(int id){
        Order order = new Order();
        order.setId(id);
        order.setStatus("2");
        orderService.updateById(order);
        return "redirect:/order/list";
    }
*/
}
